<?php 
//  淘宝登录插件如有BUG请联系本人！！ 
/*===========================================================
*   name : 淘宝
*   author : `Jn.Too'
*   QQ : 274987096
*   E-mail : 274987096@qq.com
*   尊重作者,保留版权信息
*   版权所有 `Jn.Too'
*   使用；不允许对程序代码以任何形式任何目的的再发布。
**/

if (defined('WEBSITE') || defined('GETINFO'))
{
	global $_LANG;
	$_LANG['help']['APP_KEY'] = '在open.taobao.com 里申请的 App Key';
	$_LANG['help']['APP_SECRET'] = '请注意填写，最长的就填此处';
	
	$_LANG['APP_KEY'] = 'App Key';
	$_LANG['APP_SECRET'] = 'App Secret';
	
	$i = isset($web) ? count($web) : 0;
	
	// 类名
	$web[$i]['name'] = '淘宝';
	
	
	$web[$i]['className'] = 'taobao';
	
	
	// 文件名，不包含后缀
	$web[$i]['type'] = 'taobao';
	
	// 作者信息
	$web[$i]['author'] = '`Jn.Too\'';
	
	// 作者QQ
	$web[$i]['qq'] = '274987096';
	
	// 作者邮箱
	$web[$i]['email'] = '274987096@qq.com';
	
	// 申请网址
	$web[$i]['website'] = 'http://open.taobao.com';
	
	// 版本号
	$web[$i]['version'] = '1.1v';
	
	// 更新日期
	$web[$i]['date']  = '2012-2-23';
	
	// 配置信息
	$web[$i]['config'] = array(
		array('type'=>'text' , 'name'=>'APP_KEY', 'value'=>''),
		array('type'=>'text' , 'name' => 'APP_SECRET' , 'value' => ''),
	);
}

if (!defined('WEBSITE'))
{
	include_once(dirname(__FILE__).'/oath2.class.php');
	class website extends oath2
	{
		function website()
		{
			$this->authorizeURL = 'https://oauth.taobao.com/authorize';
			$this->app_key = APP_KEY;
			$this->app_secret = APP_SECRET;
			$this->meth = 'POST';
			$this->tokenURL = 'https://oauth.taobao.com/token';
			$this->userURL = 'http://gw.api.taobao.com/router/rest';
			$this->post_msg = array(
				'fields' => 'user_id,uid,nick,sex,buyer_credit,seller_credit,location,created,last_visit,birthday,type,status,alipay_no,alipay_account,alipay_account,email,consumer_protection,alipay_bind',
				'app_key' => $this->app_key,
				'v' => '2.0',
				'format' =>'json',
				'sign_method'=>'md5',
				'method'=>'taobao.user.get',
				'timestamp'=>date('Y-m-d H:i:s'),
				'partner_id'=>'top-sdk-php-20111220',
				'session'=>'t',
				'client_id'=>'',
				'client_secret'=>'',
				'access_token'=>'',
				'refresh_token'=>'',
				'taobao_user_id'=>'',
				'taobao_user_nick'=>'',
				'w1_expires_in'=>'',
				're_expires_in'=>'',
				'expires_in'=>'',
				'token_type'=>'',
				'r1_expires_in'=>'',
			);
		}
		
		// 签名程序
		
		function sign( &$params )
		{
			$params['session'] = $this->token['access_token'];
			ksort($params);
			$stringToBeSigned = APP_SECRET;
			foreach ($params as $k => $v)
			{
				if("@" != substr($v, 0, 1))
				{
					$stringToBeSigned .= "$k$v";
				}
			}
			$stringToBeSigned .= APP_SECRET;
			$params['sign'] = strtoupper(md5($stringToBeSigned));
			
			$fields = $params['fields'];
			unset($params['fields']);
			$this->userURL = $this->userURL . '?' . http_build_query($params);
			$params = array('fields' => $fields);
		}
		
		function message( $user_info )
		{
			$arr = array();
			$user_info = $user_info['user_get_response']['user'];
			
			$arr['email'] = $user_info['email'];
			$arr['user_id'] = $user_info['user_id'];
			$arr['name']  = $user_info['nick'];
			$arr['location'] = (isset($user_info['location']['state']) ? $user_info['location']['state'] : '' ) .' '. (isset($user_info['location']['city']) ? $user_info['location']['city'] : '');
			$arr['sex'] = (isset($user_info['sex']) ? ($user_info['sex'] == 'm' ? '1' : '0') : 0);
			$arr['rank_id'] = RANK_ID;
			$arr['user_info'] = $user_info;
			return $arr;
		}
		
		function is_error( $result )
		{
			$msg = json_decode($result , true);
			if( !empty($msg['error_response'])  || !empty($msg['error']))
			{
				$msg = isset($msg['error_response']) ? $msg['error_response'] : $token['error_description'];
			}else{
				return $msg;
			}
			if(is_array($msg))
				$this->add_error($msg['code'], $msg['msg'] , '');
			return false;
		}
	}
}
?>